package cn.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import news.News;
import news.NewsDao;
import news.Page;
import news.Topic;
import news.TopicDao;

public class IndexServlet extends HttpServlet {

	public IndexServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		List<Topic> topiclist=TopicDao.DAO.findALL();
		int pageNo = 1;
		Long tid=null;
		try {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		} catch (Exception e) {
		}
		try {
			tid = Long.parseLong(request.getParameter("tid"));
		} catch (Exception e) {
		}
		Page<News> page=null;
		if (tid==null) 
		{
			page = NewsDao.DAO.paging(pageNo);
		}
		else 
		{
			page = NewsDao.DAO.paging(tid, pageNo);
		}
		request.setAttribute("page",page);
		request.setAttribute("tid",tid);
		request.setAttribute("topiclist", topiclist);
		request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		
	}

	public void init() throws ServletException {
	}
}
